package com.itheima.a23.sub;

import org.springframework.core.GenericTypeResolver;
import org.springframework.core.ResolvableType;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;

public class TestGenericType {
    public static void main(String[] args) {
        // 小技巧
        // 1. java api
        System.out.println(">>>>>>>>> java api >>>>>>>>>>>>>>");
        Type type = TeacherDao.class.getGenericSuperclass();
        System.out.println(type);

        if (type instanceof ParameterizedType parameterizedType) {
            System.out.println(parameterizedType.getActualTypeArguments()[0]);
        }

        System.out.println(">>>>>>>> SchoolDao java api >>>>>>>>>>>>>>>");
        Type schoolType = SchoolDao.class.getGenericSuperclass();
        System.out.println(schoolType);

        if (schoolType instanceof ParameterizedType parameterizedType) {
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            System.out.println(Arrays.toString(actualTypeArguments));
        }

        System.out.println(">>>>>>>> SocialDao java api >>>>>>>>>>>>>>>");
        Type socialType = SocialDao.class.getGenericSuperclass();
        System.out.println(socialType);

        if (socialType instanceof ParameterizedType parameterizedType) {
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            System.out.println(Arrays.toString(actualTypeArguments));
        }

        // 2. spring api 1
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");
        Class<?> t = GenericTypeResolver.resolveTypeArgument(TeacherDao.class, BaseDao.class);
        System.out.println(t);

        // 3. spring api 2
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(ResolvableType.forClass(TeacherDao.class).getSuperType().getGeneric().resolve());

        // 4. spring api 1  多个泛型类型
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");
        Class<?>[] classes = GenericTypeResolver.resolveTypeArguments(SchoolDao.class, BaseTwoDao.class);
        System.out.println(Arrays.toString(classes));

        // 5. spring api 2
        System.out.println(">>>>>>>>>>> spring api 2 >>>>>>>>>>>>");
        ResolvableType[] generics = ResolvableType.forClass(SchoolDao.class).getSuperType().getGenerics();
        for (ResolvableType generic : generics) {
            System.out.println(generic.resolve());
        }
        // 5. spring api 2
        System.out.println(">>>>>>>>>>> spring api 2 >>>>>>>>>>>>");
        ResolvableType[] socialGenerics = ResolvableType.forClass(SocialDao.class).getSuperType().getGenerics();
        for (ResolvableType generic : socialGenerics) {
            System.out.println(generic.resolve());
        }


        // 4. spring api 1  多个泛型类型
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");
        Class<?>[] classes2 = GenericTypeResolver.resolveTypeArguments(SocialDao.class, BaseThreeDao.class);
        System.out.println(Arrays.toString(classes2));

    }

}
